//
//  PhotoAlbumViewController.m
//  PhotoGirl
//
//  Created by comic on 4/16/13.
//  Copyright (c) 2013 comic. All rights reserved.
//

#import "AlbumDetailViewController.h"
#import <ASIHTTPRequest/ASIHTTPRequest.h>
#import "IoObjectRequest.h"
#import "AlbumEntity.h"
#import "PhotoEntity.h"
#import "ImageViewController.h"


@interface AlbumDetailViewController ()

@end

@implementation AlbumDetailViewController
@synthesize photos=_photos;
-(void) addRefreshButton:(BOOL)animated loading:(BOOL) loading {
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    if (!loading) {
        self.navigationItem.rightBarButtonItem=self.refreshButton;
    }else {
        self.navigationItem.rightBarButtonItem=self.loadingButton;
    }
    
}
-(void) didFinishQueue {
    [(UIActivityIndicatorView *)self.loadingButton.customView stopAnimating];
    self.navigationItem.rightBarButtonItem=self.refreshButton;
//    [self.collectionView reloadData];
}
- (void)requestDone:(IoObjectRequest *)request
{
    // truong hop load json
    if([request.interfaceObject isKindOfClass:[AlbumDetailViewController class]]) {
        NSData *response = [request responseData];
        NSError *error=nil;
        NSArray *albumArray=[NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableContainers error:&error];
        if (!error) {
            NSMutableArray *album=[[NSMutableArray alloc] init];
            NSMutableArray *photos=[[NSMutableArray alloc] init];
            for(int i=0; i<albumArray.count; i++) {
                if (i>0) {
                    AlbumEntity *entity=[[AlbumEntity alloc] init];
                    NSDictionary *jsonAlbum=[albumArray objectAtIndex:i];
                    entity.albumID=[jsonAlbum[ALBUM_ID] integerValue];
                    entity.albumName=jsonAlbum[ALBUM_NAME];
                    entity.albumURL=[NSString stringWithFormat:@"%@%@&WID=100&HEI=100&CVT=JPEG",kPhotoBaseURL,jsonAlbum[@"dir"]];
                    entity.imageAlbum=[[UIImageView alloc]init];
                    [album addObject:entity];
                    PhotoEntity *photo=[[PhotoEntity alloc] init];
                    photo.imageId=jsonAlbum[@"id"];
                    photo.imageName=jsonAlbum[@"dir"];
                    photo.tileX=[jsonAlbum[@"x_tile"] integerValue];
                    photo.tileY=[jsonAlbum[@"y_tile"] integerValue];
                    photo.maxWidth=[jsonAlbum[@"width"] integerValue];
                    photo.maxHeight=[jsonAlbum[@"height"] integerValue];
                    [photos addObject:photo];
                }
            }
            [self setPhotoAlbum:album];
            [self setPhotos:photos];
            [self loadAlbumImage];
        }else {
            [[[UIAlertView alloc] initWithTitle:@"Lỗi trong quá trình tải album"
                                        message:error.localizedDescription
                                       delegate:nil
                              cancelButtonTitle:@"Đồng ý"
                              otherButtonTitles:nil] show];
            
        }
    }else {
        [super requestDone:request];
    }
    
}


// DELEGATE
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    AlbumEntity *album=[self.photoAlbum objectAtIndex:indexPath.row];
    PhotoEntity *photo=[self.photos objectAtIndex:indexPath.row];
    ImageViewController *controller=(ImageViewController* )[self.navigationController.storyboard instantiateViewControllerWithIdentifier:@"scrollAlbumViewController"];
    controller.navigationItem.title=album.albumName;
    [controller setPhoto:photo];
//    controller.photoAlbumUrl=[NSString stringWithFormat:@"%@list_photo?album_id=%d",kPhotoAPIUrl,album.albumID];
    [self.navigationController pushViewController:controller animated:YES];
}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
- (void)viewDidUnload {
    [super viewDidUnload];
}
@end
